home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Burning & Media / GB-PVR 1.2.13 / GBPVR10213.msi / Cabs.w1.cab / SearchEngine.cs346 < prev    next >
Encoding:
Text File  |  2008-01-22  |  29.4 KB  |  648 lines

  1. using System;
  2. using System.Collections;
  3. using System.Data.Common;
  4. using System.Web;
  5. using GBPVR.Public;
  6. using GBPVRSchedule;
  7.  
  8. /// <summary>
  9. /// Summary description for Search
  10. /// </summary>
  11. /// 
  12.  
  13. namespace gbweb.classes
  14. {
  15.     public class SearchEngine : IDisposable
  16.     {
  17.         //Contains the list of programmes that are found to match the users search criteria
  18.         private ArrayList ProgrammeArray = new ArrayList();
  19.  
  20.         //Reader used to access the database
  21.         private DbDataReader aReader;
  22.  
  23.         //Used to pull programmes based off of passed in date and time filter values
  24.         private DateTime startListingTime = new DateTime();
  25.         private DateTime endListingTime = new DateTime();
  26.  
  27.         //Holds passed in search date and time filter values
  28.         private string startdate = string.Empty;
  29.         private string starttime = string.Empty;
  30.         private string enddate = string.Empty;
  31.         private string endtime = string.Empty;
  32.  
  33.         //Intialize the working search dates to dummy values
  34.         DateTime startDate = Convert.ToDateTime("01/01/1901");
  35.         DateTime startTime = Convert.ToDateTime("01:01 AM");
  36.         DateTime endDate = Convert.ToDateTime("01/01/1901");
  37.         DateTime endTime = Convert.ToDateTime("01:01 AM");
  38.         
  39.         public SearchEngine()
  40.         {
  41.             //
  42.             // TODO: Add constructor logic here
  43.             //
  44.         }
  45.  
  46.         #region IDisposable Members
  47.  
  48.         public void Dispose()
  49.         {
  50.             ProgrammeArray.Clear();
  51.         }
  52.  
  53.         #endregion
  54.  
  55.         //This method initializes and sets the search dates that are needed to process and filter the search
  56.         public void setSearchDateTime(string searchStartDate, string searchStartTime, string searchEndDate, string searchEndTime)
  57.         {
  58.             startdate = searchStartDate;
  59.             starttime = searchStartTime;
  60.             enddate = searchEndDate;
  61.             endtime = searchEndTime;
  62.  
  63.             //Set the working search dates from the passed in string based values
  64.             if (startdate != "01/01/1901")
  65.             {
  66.                 if (starttime != "01:01 AM")
  67.                 {
  68.                     startDate = Convert.ToDateTime(startdate);
  69.                     startTime = Convert.ToDateTime(starttime);
  70.                 }
  71.                 else
  72.                 {
  73.                     startDate = Convert.ToDateTime(startdate);
  74.                 }
  75.             }
  76.             else
  77.             {
  78.                 if (starttime != "01:01 AM")
  79.                 {
  80.                     startTime = Convert.ToDateTime(starttime);
  81.                 }
  82.             }
  83.  
  84.             if (enddate != "01/01/1901")
  85.             {
  86.                 if (endtime != "01:01 AM")
  87.                 {
  88.                     endDate = Convert.ToDateTime(enddate);
  89.                     endTime = Convert.ToDateTime(endtime);
  90.                 }
  91.                 else
  92.                 {
  93.                     endDate = Convert.ToDateTime(enddate);
  94.                 }
  95.             }
  96.             else
  97.             {
  98.                 if (endtime != "01:01 AM")
  99.                 {
  100.                     endTime = Convert.ToDateTime(endtime);
  101.                 }
  102.             }
  103.  
  104.             //Initialize the search base date span 
  105.             startListingTime = DateTime.Now;
  106.             endListingTime = startListingTime.AddDays(30);
  107.  
  108.             //This is the code to that will update the startListingTime and endListingTime to contain search base start/end date values for pulling recordings
  109.             if (startDate.Year != 1901)
  110.             {
  111.                 if (startTime.ToString("HH:mm") != "01:01")
  112.                 {
  113.                     startListingTime = startDate.Date + startTime.TimeOfDay;
  114.                 }
  115.                 else
  116.                 {
  117.                     DateTime tmp = DateTime.Now.Date;
  118.                     startListingTime = startDate.Date + tmp.TimeOfDay;
  119.                 }
  120.             }
  121.             else
  122.             {
  123.                 if (startTime.ToString("HH:mm") != "01:01")
  124.                 {
  125.                     startListingTime = DateTime.Now.Date + startTime.TimeOfDay;
  126.                 }
  127.                 else
  128.                 {
  129.                     startListingTime = DateTime.Now;
  130.                 }
  131.             }
  132.  
  133.             if (endDate.Year != 1901)
  134.             {
  135.                 if (endTime.ToString("HH:mm") != "01:01")
  136.                 {
  137.                     endListingTime = endDate.Date + endTime.TimeOfDay;
  138.                 }
  139.                 else
  140.                 {
  141.                     DateTime tmp = DateTime.Now.Date;
  142.                     endListingTime = endDate.Date + tmp.AddHours(23).AddMinutes(59).TimeOfDay;
  143.                 }
  144.             }
  145.             else
  146.             {
  147.                 if (endTime.ToString("HH:mm") != "01:01")
  148.                 {
  149.                     endListingTime = DateTime.Now.AddDays(30).Date + endTime.TimeOfDay;
  150.                 }
  151.                 else
  152.                 {
  153.                     DateTime tmp = DateTime.Now.Date;
  154.                     endListingTime = DateTime.Now.AddDays(30).Date + tmp.AddHours(23).AddMinutes(59).TimeOfDay;
  155.                 }
  156.             }
  157.  
  158.             int CmprDt = DateTime.Compare(startListingTime, endListingTime);
  159.  
  160.             //Check to see if the user entered a start date that is greater than the end date and if so set the start date to be 30 days prior to the end date
  161.             if (CmprDt > 0)
  162.             {
  163.                 startListingTime = endListingTime.AddDays(-30);
  164.             }
  165.         }
  166.  
  167.         //This method is where programmes are pulled from the database using a query based on passed in user criteria.
  168.         //User filters that can not be applied via the query process are provided via the filterSerch method wich is called prior to this
  169.         //method returning the arraylist of programmes to the calling class.
  170.         public ArrayList SelectProgrammes(Array channels, Array selectedGenre, bool checkTitle, bool checkDesc, bool checkSubtitle, bool matchTitle, bool matchDesc, bool matchSubtitle, bool matchUniqueID, String searchFor, bool caseSensitive, Schedule scheduleHelper, int programmeMinLength, int programmeMaxLength)
  171.         {
  172.             //Instantiate the arraylist for holding programmes
  173.             ProgrammeArray = new ArrayList();
  174.  
  175.             //If nothing was entered to search for then we want to defualt the search flags to be false
  176.             if (searchFor == null || searchFor.Length < 1)
  177.             {
  178.                 checkTitle = false;
  179.                 matchTitle = false;
  180.                 checkSubtitle = false;
  181.                 matchSubtitle = false;
  182.                 checkDesc = false;
  183.                 matchDesc = false;
  184.             }
  185.  
  186.             //For searches passed in from the detail screen we have to URL Decode it back to normal and then replace and 's with ''s
  187.             if (searchFor != null)
  188.             {
  189.                 searchFor = HttpUtility.UrlDecode(searchFor).Replace("'", "''");
  190.             }
  191.  
  192.             //Instantiate variables used in the creation of the search SQL command
  193.             string chnl_cmd;
  194.             bool chnl_filter;
  195.             string chnl_join;
  196.             string genre_cmd;
  197.             bool genre_filter;
  198.             string genre_join;
  199.             string pgm_cmd;
  200.             string where_cmd;
  201.  
  202.             initializeSQLCommands(channels, out chnl_cmd, out chnl_filter, out chnl_join, out genre_cmd, out genre_filter, out genre_join, out pgm_cmd, selectedGenre, out where_cmd);
  203.  
  204.             //Only go through this processing if wer are not matching the programme unique id
  205.             if (!matchUniqueID)
  206.             {
  207.                 //Check to see if we are searching the Title or matching on the Title
  208.                 if (checkTitle || matchTitle)
  209.                 {
  210.  
  211.                     //Check to see if we are searching the title
  212.                     if (checkTitle)
  213.                     {
  214.                         where_cmd += " and UPPER(PROGRAMME.name) like '%" + searchFor + "%'";
  215.                     }
  216.                     else
  217.                     //We are matching the title
  218.                     {
  219.                         where_cmd += " and UPPER(PROGRAMME.name) = '" + searchFor + "'";
  220.                     }
  221.  
  222.                     //Process and then execute the query
  223.                     DbConnection aConnection = processQuery(chnl_cmd, chnl_join, genre_cmd, genre_join, ref pgm_cmd, ref where_cmd);
  224.  
  225.                     // iterate through the results to load found programme(s)to the array
  226.                     while (aReader.Read())
  227.                     {
  228.                         Programme pgm = scheduleHelper.GetProgrammeByOID(aReader.GetInt32(0));
  229.                         if (!caseSensitive)
  230.                         {
  231.                             ProgrammeArray.Add(scheduleHelper.GetProgrammeByOID(aReader.GetInt32(0)));
  232.                         }
  233.                         else
  234.                         {
  235.                             if (pgm.getTitle().IndexOf(searchFor) > -1)
  236.                             {
  237.                                 ProgrammeArray.Add(pgm);
  238.                             }
  239.                         }
  240.                     }
  241.  
  242.                     //Close the reader and connection
  243.                     closeReader(aConnection);
  244.                     //Re-Initialize SQL Commands in case the user has choosen to pull programmes based on other criteria such as sub-title or description
  245.                     initializeSQLCommands(channels, out chnl_cmd, out chnl_filter, out chnl_join, out genre_cmd, out genre_filter, out genre_join, out pgm_cmd, selectedGenre, out where_cmd);
  246.                 }
  247.  
  248.                 //Check to see if we are searching the Subtitle or matching on the Subtitle
  249.                 if (checkSubtitle || matchSubtitle)
  250.                 {
  251.                     //Check to see if we are searching the subtitle
  252.                     if (checkSubtitle)
  253.                     {
  254.                         where_cmd += " and UPPER(PROGRAMME.sub_title) like '%" + searchFor + "%'";
  255.                     }
  256.                     else
  257.                     //We are matching the title
  258.                     {
  259.                         where_cmd += " and UPPER(PROGRAMME.sub_title) = '" + searchFor + "'";
  260.                     }
  261.  
  262.                     //Process and then execute the query
  263.                     DbConnection aConnection = processQuery(chnl_cmd, chnl_join, genre_cmd, genre_join, ref pgm_cmd, ref where_cmd);
  264.  
  265.                     // iterate through the results to load found programme(s)to the array
  266.                     while (aReader.Read())
  267.                     {
  268.                         Programme pgm = scheduleHelper.GetProgrammeByOID(aReader.GetInt32(0));
  269.                         if (!caseSensitive)
  270.                         {
  271.                             if (!ProgrammeArray.Contains(pgm))
  272.                             {
  273.                                 ProgrammeArray.Add(pgm);
  274.                             }
  275.                         }
  276.                         else
  277.                         {
  278.                             if (pgm.getSubTitle().IndexOf(searchFor) > -1)
  279.                             {
  280.                                 if (!ProgrammeArray.Contains(pgm))
  281.                                 {
  282.                                     ProgrammeArray.Add(pgm);
  283.                                 }
  284.                             }
  285.                         }
  286.                     }
  287.  
  288.                     //Close the reader and connection
  289.                     closeReader(aConnection);
  290.  
  291.                     //Re-Initialize SQL Commands in case the user has choosen to pull programmes based on other criteria such as sub-title or description
  292.                     initializeSQLCommands(channels, out chnl_cmd, out chnl_filter, out chnl_join, out genre_cmd, out genre_filter, out genre_join, out pgm_cmd, selectedGenre, out where_cmd);
  293.                 }
  294.  
  295.                 //Check to see if we are searching the Dexcription or matching on the Description
  296.                 if (checkDesc || matchDesc)
  297.                 {
  298.                     //Check to see if we are searching the subtitle
  299.                     if (checkSubtitle)
  300.                     {
  301.                         where_cmd += " and UPPER(PROGRAMME.description) like '%" + searchFor + "%'";
  302.                     }
  303.                     else
  304.                     //We are matching the title
  305.                     {
  306.                         where_cmd += " and UPPER(PROGRAMME.description) = '" + searchFor + "'";
  307.                     }
  308.  
  309.                     //Process and then execute the query
  310.                     DbConnection aConnection = processQuery(chnl_cmd, chnl_join, genre_cmd, genre_join, ref pgm_cmd, ref where_cmd);
  311.  
  312.                     // iterate through the results to load found programme(s)to the array
  313.                     while (aReader.Read())
  314.                     {
  315.                         Programme pgm = scheduleHelper.GetProgrammeByOID(aReader.GetInt32(0));
  316.                         if (!caseSensitive)
  317.                         {
  318.                             if (!ProgrammeArray.Contains(pgm))
  319.                             {
  320.                                 ProgrammeArray.Add(pgm);
  321.                             }
  322.                         }
  323.                         else
  324.                         {
  325.                             if (pgm.getDescription().IndexOf(searchFor) > -1)
  326.                             {
  327.                                 if (!ProgrammeArray.Contains(pgm))
  328.                                 {
  329.                                     ProgrammeArray.Add(pgm);
  330.                                 }
  331.                             }
  332.                         }
  333.                     }
  334.  
  335.                     //Close the reader and connection
  336.                     closeReader(aConnection);
  337.  
  338.                     //Re-Initialize SQL Commands in case the user has choosen to pull programmes based on other criteria such as sub-title or description
  339.                     initializeSQLCommands(channels, out chnl_cmd, out chnl_filter, out chnl_join, out genre_cmd, out genre_filter, out genre_join, out pgm_cmd, selectedGenre, out where_cmd);
  340.                 }
  341.             }
  342.             else
  343.             //We are searching for a unique programme via the unique programme identifier
  344.             {
  345.                 ProgrammeArray.Clear();
  346.  
  347.                 where_cmd += " and unique_identifier = '" + searchFor + "'";
  348.                 DbConnection aConnection = executeQuery(pgm_cmd, where_cmd);
  349.  
  350.                 // iterate through the results to load found programme(s)to the array
  351.                 while (aReader.Read())
  352.                 {
  353.                     ProgrammeArray.Add(scheduleHelper.GetProgrammeByOID(aReader.GetInt32(0)));
  354.                 }
  355.  
  356.                 closeReader(aConnection);
  357.             }
  358.  
  359.             //If the user has not selected to reduce the search by using something in the title, sub-title or description we need to load up all found
  360.             //programmes in the timeperiod.
  361.             if (checkTitle || checkSubtitle || checkDesc || matchTitle || matchDesc || matchSubtitle || matchUniqueID)
  362.             {
  363.                 //Do nothing since we have narrowed the returned programmes to something we were searching on from the Programme table
  364.             }
  365.             else
  366.             {
  367.                 //Since the user didn't filter on any text check to see if they are filtering on the Channel or Genre
  368.                 if (chnl_filter || genre_filter)
  369.                 {
  370.                     //We are filtering on both the Channel and Genre
  371.                     if (chnl_filter && genre_filter)
  372.                     {
  373.                         pgm_cmd += " " + chnl_join;
  374.                         where_cmd += " and " + chnl_cmd;
  375.                         pgm_cmd += " " + genre_join;
  376.                         where_cmd += " and " + genre_cmd;
  377.                         DbConnection aConnection = executeQuery(pgm_cmd, where_cmd);
  378.                         // iterate through the results to load found programme(s)to the array
  379.                         while (aReader.Read())
  380.                         {
  381.                             ProgrammeArray.Add(scheduleHelper.GetProgrammeByOID(aReader.GetInt32(0)));
  382.                         }
  383.                         closeReader(aConnection);
  384.                     }
  385.                     else
  386.                     {
  387.                         //We are filtering on just the Channel
  388.                         if (chnl_filter)
  389.                         {
  390.                             pgm_cmd += " " + chnl_join;
  391.                             where_cmd += " and " + chnl_cmd;
  392.                             DbConnection aConnection = executeQuery(pgm_cmd, where_cmd);
  393.                             // iterate through the results to load found programme(s)to the array
  394.                             while (aReader.Read())
  395.                             {
  396.                                 ProgrammeArray.Add(scheduleHelper.GetProgrammeByOID(aReader.GetInt32(0)));
  397.                             }
  398.                             closeReader(aConnection);
  399.                         }
  400.                         else
  401.                         //We are filtering on just the Genre
  402.                         {
  403.                             pgm_cmd += " " + genre_join;
  404.                             where_cmd += " and " + genre_cmd;
  405.                             DbConnection aConnection = executeQuery(pgm_cmd, where_cmd);
  406.                             // iterate through the results to load found programme(s)to the array
  407.                             while (aReader.Read())
  408.                             {
  409.                                 ProgrammeArray.Add(scheduleHelper.GetProgrammeByOID(aReader.GetInt32(0)));
  410.                             }
  411.                             closeReader(aConnection);
  412.                         }
  413.                     }
  414.                 }
  415.                 else
  416.                 //The user is only filtering on the date and or time so load up all progammes that fall into the date/time range
  417.                 {
  418.                     DbConnection aConnection = executeQuery(pgm_cmd, where_cmd);
  419.                     while (aReader.Read())
  420.                     {
  421.                         ProgrammeArray.Add(scheduleHelper.GetProgrammeByOID(aReader.GetInt32(0)));
  422.                     }
  423.                     closeReader(aConnection);
  424.                 }
  425.             }
  426.             //We need to check to see if there were additional search filtering that needs to be applied to the results that could not be applied
  427.             //within the query itself
  428.             filterSearchResults(programmeMinLength, programmeMaxLength);
  429.  
  430.             //Return the Array that contains the programmes that matched all the search critera the user entered
  431.             return ProgrammeArray;
  432.         }
  433.  
  434.         //This method is used to initialize the base SQL commands used in the search query command
  435.         private void initializeSQLCommands(Array channels, out string chnl_cmd, out bool chnl_filter, out string chnl_join, out string genre_cmd, out bool genre_filter, out string genre_join, out string pgm_cmd, Array selectedGenre, out string where_cmd)
  436.         {
  437.             //Instantiate the bare minimum SQL command lines that will be used in the query
  438.             pgm_cmd = "SELECT PROGRAMME.oid FROM PROGRAMME";
  439.             where_cmd = " where PROGRAMME.start_time >= datetime('" + startListingTime.ToString("yyyy-MM-ddTHH:mm:ss") + "') and PROGRAMME.end_time <= datetime('" + endListingTime.ToString("yyyy-MM-ddTHH:mm:ss") + "')";
  440.  
  441.             //Instantiate the variables used in building the extended refinement SQL command strings
  442.  
  443.             //Build the static refinement SQL command strings
  444.             build_Refinement_Query_Commands(channels, out chnl_cmd, out chnl_filter, out chnl_join, out genre_cmd, out genre_filter, out genre_join, selectedGenre);
  445.         }
  446.  
  447.         //This method is used to create the extended parts of the query command that may be used to refine the search query SQL
  448.         private static void build_Refinement_Query_Commands(Array channels, out string chnl_cmd, out bool chnl_filter, out string chnl_join, out string genre_cmd, out bool genre_filter, out string genre_join, Array selectedGenre)
  449.         {
  450.             chnl_cmd = string.Empty;
  451.             chnl_filter = false;
  452.             chnl_join = "LEFT JOIN CHANNEL ON PROGRAMME.channel_oid = CHANNEL.oid";
  453.  
  454.             genre_cmd = string.Empty;
  455.             genre_filter = false;
  456.             genre_join = "LEFT JOIN PROGRAMME_GENRE ON PROGRAMME.oid = PROGRAMME_GENRE.programme_oid LEFT JOIN GENRE ON GENRE.oid = PROGRAMME_GENRE.genre_oid";
  457.  
  458.             //Build the command for filtering by Channel if the user selected any channels to filter on
  459.             if (channels != null && channels.Length > 0)
  460.             {
  461.                 chnl_cmd = "CHANNEL.channel_number IN(";
  462.                 chnl_filter = true;
  463.                 foreach (string chnl in channels)
  464.                 {
  465.                     chnl_cmd += Convert.ToInt32(chnl) + ",";
  466.                 }
  467.                 int idx = chnl_cmd.LastIndexOf(",");
  468.                 chnl_cmd = chnl_cmd.Remove(idx);
  469.                 chnl_cmd += ")";
  470.             }
  471.  
  472.             //Build the command for filtering by Genre if the user selected any genre to filter on
  473.             if (selectedGenre != null && selectedGenre.Length > 0)
  474.             {
  475.                 genre_cmd = "GENRE.genre_name IN(";
  476.                 genre_filter = true;
  477.                 foreach (string genre in selectedGenre)
  478.                 {
  479.                     genre_cmd += "'" + genre + "',";
  480.                 }
  481.                 int idx = genre_cmd.LastIndexOf(",");
  482.                 genre_cmd = genre_cmd.Remove(idx);
  483.                 genre_cmd += ")";
  484.             }
  485.         }
  486.  
  487.         //This method is used to generate the final query command and to execute the query
  488.         private DbConnection processQuery(string chnl_cmd, string chnl_join, string genre_cmd, string genre_join, ref string pgm_cmd, ref string where_cmd)
  489.         {
  490.             pgm_cmd = refineSearchQuery(chnl_cmd, chnl_join, genre_cmd, genre_join, pgm_cmd, ref where_cmd);
  491.  
  492.             //Execute the completed query
  493.             return executeQuery(pgm_cmd, where_cmd);
  494.         }
  495.  
  496.         //This method is where we may add more parameters to the query string to help further refine the search query
  497.         private static string refineSearchQuery(string chnl_cmd, string chnl_join, string genre_cmd, string genre_join, string pgm_cmd, ref string where_cmd)
  498.         {
  499.             //Check to see if we are refining the searh by Channel or Genre
  500.             if (chnl_cmd != string.Empty || genre_cmd != string.Empty)
  501.             {
  502.                 if (chnl_cmd != string.Empty && genre_cmd != string.Empty)
  503.                     //We are refining the search by both Channel and Genre
  504.                 {
  505.                     pgm_cmd += " " + chnl_join;
  506.                     where_cmd += " and " + chnl_cmd;
  507.                     pgm_cmd += " " + genre_join;
  508.                     where_cmd += " and " + genre_cmd;
  509.  
  510.                 }
  511.                 else
  512.                     //We are only refining the search by either the Channel or Genre
  513.                 {
  514.                     if (chnl_cmd != string.Empty)
  515.                         //We are refining the search just by Channel
  516.                     {
  517.                         pgm_cmd += " " + chnl_join;
  518.                         where_cmd += " and " + chnl_cmd;
  519.                     }
  520.                     else
  521.                         //We are refining the search just by Genre
  522.                     {
  523.                         pgm_cmd += " " + genre_join;
  524.                         where_cmd += " and " + genre_cmd;
  525.                     }
  526.                 }
  527.             }
  528.             return pgm_cmd;
  529.         }
  530.  
  531.         //This method is where any filtering that can not be done via the raw query should be performed.
  532.         //The method clears and reloads the arraylist of programmes that were found with the query to only contain programmes that still meet the 
  533.         //search criteria following the check of these non-query based filter parameters
  534.         private void filterSearchResults(int minLength, int maxLength)
  535.         {
  536.  
  537.             //We only need to do this processing for things that have not already been taken care of by the parameters applied in the queries.
  538.             //Therefore if we are not using any of these additional search filters there is no sense in adding the extra overhead with additional
  539.             //processing.  To do this we check all filter parameters that are to be checked in this method to see if they are equal to their default values.
  540.  
  541.             if (!((minLength == int.MinValue) && (maxLength == int.MaxValue) && (startdate == "01/01/1901") && (enddate == "01/01/1901") && (starttime == "01:01 AM")
  542.                   && (endtime == "01:01 AM")))
  543.             {
  544.                 //Instantiate a new temporary arraylist to process the remaiing filter criteria
  545.                 ArrayList pgmArray = new ArrayList(ProgrammeArray);
  546.  
  547.                 //Clear the arralist of found programmes since we will be reloading with programmes that meet any filtering we need to apply
  548.                 ProgrammeArray.Clear();
  549.  
  550.                 //Set the checksum of the start and end dates that is used in the filter process
  551.                 int parmStartDateSum = startDate.Month + startDate.Day + startDate.Year;
  552.                 int parmEndDateSum = endDate.Month + endDate.Day + endDate.Year;
  553.  
  554.                 //Date processing logic to compare programmes that have already matched a entered search parameter
  555.                 foreach (Programme program in pgmArray)
  556.                 {
  557.                     bool showProgram = true;
  558.  
  559.                     int pgmStartDateSum = program.getStartTime().Month + program.getStartTime().Day +
  560.                                           program.getStartTime().Year;
  561.                     int pgmEndDateSum = program.getEndTime().Month + program.getEndTime().Day +
  562.                                         program.getEndTime().Year;
  563.  
  564.                     double length = (program.getEndTime() - program.getStartTime()).TotalMinutes;
  565.                     showProgram &= (length >= minLength) && (length <= maxLength);
  566.  
  567.                     if (showProgram)
  568.                     {
  569.                         //Check to see if the passed in start date is a dummy date
  570.                         if (parmStartDateSum != 1903)
  571.                         {
  572.                             if (pgmStartDateSum < parmStartDateSum)
  573.                             {
  574.                                 showProgram = false;
  575.                             }
  576.                         }
  577.  
  578.                         //Check to see if the passed in start time is a dummy time
  579.                         if (showProgram && startTime.ToString("HH:mm") != "01:01")
  580.                         {
  581.                             int Cmpr =
  582.                                 String.Compare(program.getStartTime().ToString("HH:mm"), startTime.ToString("HH:mm"));
  583.  
  584.                             if (Cmpr < 0)
  585.                             {
  586.                                 showProgram = false;
  587.                             }
  588.                         }
  589.  
  590.                         //Check to see if the passed in end date is a dummy date
  591.                         if (showProgram && parmEndDateSum != 1903)
  592.                         {
  593.                             if (pgmEndDateSum > parmEndDateSum)
  594.                             {
  595.                                 showProgram = false;
  596.                             }
  597.                         }
  598.  
  599.                         //Check to see if the passed in end time is a dummy time
  600.                         if (showProgram && endTime.ToString("HH:mm") != "01:01")
  601.                         {
  602.                             int Cmpr =
  603.                                 String.Compare(program.getEndTime().ToString("HH:mm"), endTime.ToString("HH:mm"));
  604.  
  605.                             if (Cmpr > 0)
  606.                             {
  607.                                 showProgram = false;
  608.                             }
  609.                         }
  610.                     }
  611.  
  612.                     if (showProgram)
  613.                     {
  614.                         ProgrammeArray.Add(program);
  615.                     }
  616.                 }
  617.  
  618.                 //Empty the temporary programme array since we are done with it
  619.                 pgmArray.Clear();
  620.             }
  621.         }
  622.  
  623.         //This method creates the connection and executes the query
  624.         private DbConnection executeQuery(string pgm_cmd, string where_cmd)
  625.         {
  626.             // create the database connection
  627.             DbConnection aConnection = Global.GetOpenGBPVRDbConnection();
  628.             // create the command object and store the sql query
  629.             DbCommand aCommand = aConnection.CreateCommand();
  630.             //Create the command sql query string
  631.             aCommand.CommandText = pgm_cmd + where_cmd;
  632.             aCommand.Connection = aConnection;
  633.             // create the datareader object to connect to table and execute the query
  634.             aReader = aCommand.ExecuteReader();
  635.             return aConnection;
  636.         }
  637.  
  638.         //This method closes the reader that was being used as well as the connection that was being used
  639.         private void closeReader(DbConnection aConnection)
  640.         {
  641.             // close the reader
  642.             aReader.Close();
  643.             //close the connection
  644.             aConnection.Close();
  645.         }
  646.     }
  647. }
  648.